package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取用户id
        Long userid = BaseContext.getCurrentId();
        //判断菜品或者套餐是否被点过
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(ShoppingCart::getUserId, userid);
        //判断用户点的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId==null){
        //菜品为空，说明是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
         //不为空,说明是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        //调用方法
        //查询本菜品或者套餐有没有点过
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one!=null){
            //不为空，说明存在，数量加1
            one.setNumber(one.getNumber()+1);
            //修改
            shoppingCartService.updateById(one);
            return R.success(one);
        }else {
            //为空，不存在，添加
            //数量为1
            shoppingCart.setNumber(1);
            //设置id
            shoppingCart.setUserId(userid);
            //保存
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        //调用方法
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 取消购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R subShoppingCart(@RequestBody ShoppingCart shoppingCart){

        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //获取用户id
        Long userid = BaseContext.getCurrentId();
        queryWrapper.eq(ShoppingCart::getUserId,userid);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //判断是菜品还是套餐
        if(dishId!=null){
            //是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //调用方法
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        //获取菜品或者套餐的数量然后进行减1操作
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        //判断减1操作后number的数量
        if(number>0){
            //进行修改操作
            shoppingCartService.updateById(one);
        }else {
            //number是小于0,进行删除操作
            shoppingCartService.remove(queryWrapper);
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     * @param
     * @return
     */
    @DeleteMapping("/clean")
    public R delete(){
        Long userId = BaseContext.getCurrentId();
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        //调用方法清空
        shoppingCartService.remove(queryWrapper);
        return R.success("清除成功");
    }
}
